********//dofile for obtianing age-earnings profile from ACS********
use "$data/ACS/usa_00029", clear

//merge on labor force participation
merge 1:1 serial pernum year using "$data/ACS/usa_00039", keep(match) nogen

//sample restrictions
keep if age>=19 & age<=65 //restricted age range
keep if relate == 1 //household heads

//create hours variable
gen hours = uhrswork * wkswork1
gen wage = incwage / hours //hourly wage

//hours restrictions
drop if educ<6 //at least HS

//college dummy
gen coll = (educ>=10)
gen coll2 = (educd>=71)
gen hs = (educ == 6) //high school dummy
drop if educd == 61 //no HS diploma
drop if educ == 11 //remove advanced degrees!

//educational attainment
gen educ_cat = 0
replace educ_cat = 1 if educd<80
replace educ_cat = 2 if educd == 81
replace educ_cat = 3 if educd>81


//variable restriction
keep incwage wage hours coll coll2 age year perwt hs hours educ_cat school empstat serial pernum
compress

//deflate money stuff
merge m:1 year using "$data/GDP/gdp_pce_deflator", keep(match) nogen
replace deflator = deflator/100 //normalize
replace incwage = incwage/deflator //deflate labor income
replace wage = wage/deflator //now in 2012 dollars

//sample restrictions

//convert to 2000 dollars
gen deflator_2000 = 0.78235
gen incwage_2000 = incwage * deflator_2000
drop if age>30 & (hours<520 | incwage_2000<6000) & empstat == 1 //adequate work for older heads
drop if age<=30 & (hours<260 | incwage_2000<4000) & empstat == 1 //adequate work for older heads
drop if hours>5820 //too many hours

//normalizing wage
//su wage if hs & age == 19 & hours>1560 [fw=perwt] //average wage for 19-year old conditional on strong LF attachment
//keep if age>=23 //restricted age range

//convert stuff to logs
//gen lwage = log(wage)
//gen loginc = log(incwage)

replace school = school - 1
drop if school == 1 & age>22 //nix people who are in school after certain age. 

//save smaller version
save "$temp/census_profile_data", replace



//collapsing


********open********
use "$temp/census_profile_data", clear
gen emp = (empstat == 1)
gen unemp = (empstat == 2)
gen nilf = (empstat == 3)

//bins for income among working folks
preserve
keep if empstat == 1
replace incwage = incwage/40000
collapse (mean) inc_mean = incwage (sd) inc_sd = incwage [fw = perwt], by(age educ_cat) //big collapse statement. 
gen inc_var = inc_sd^2
save "$temp/census_age_profiles_income", replace
restore


//bins for employment and LFP rates
collapse (mean) emp unemp nilf, by(age educ_cat)
save "$temp/census_age_profiles_emp", replace

***********
/*
use "$temp/census_age_profiles_income", clear
drop if age<23 //age restriction

line inc_mean age if educ_cat == 1 || ///
line inc_mean age if educ_cat == 2 || ///
line inc_mean age if educ_cat == 3


line inc_sd age if educ_cat == 1 || ///
line inc_sd age if educ_cat == 2 || ///
line inc_sd age if educ_cat == 3



use "$temp/census_age_profiles_emp", clear
drop if age<23 //age restriction

line emp age if educ_cat == 1 || ///
line emp age if educ_cat == 2 || ///
line emp age if educ_cat == 3

line unemp age if educ_cat == 1 || ///
line unemp age if educ_cat == 2 || ///
line unemp age if educ_cat == 3

line nilf age if educ_cat == 1 || ///
line nilf age if educ_cat == 2 || ///
line nilf age if educ_cat == 3
*/

use "$temp/census_age_profiles_income", clear
drop if age<23 //age restriction
drop inc_var
reshape wide inc_mean inc_sd, i(age) j(educ_cat)
save "$temp/census_wage_profiles", replace
export delimited "$model/moments/age_profiles_income.csv", novarn replace

use "$temp/census_age_profiles_emp", clear
drop if age<23 //age restriction
reshape wide emp unemp nilf, i(age) j(educ_cat)
save "$temp/census_emp_profiles", replace
export delimited "$model/moments/age_profiles_emp.csv", novarn replace



	
//end of dofile